define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        refundinfo: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'select/storage/refundinfo' + location.search,
                    multi_url: 'select/storage/multi',
                    table: 'mt_dish_storage_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'oi_id',
                sortName: 'oi_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams:function(params){
                    var filter=JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var oi_order_id=Fast.api.query('oi_order_id');
                    if (oi_order_id){
                        filter.oi_order_id=oi_order_id;
                        op.oi_order_id='=';
                        params.filter=JSON.stringify(filter);
                        params.op = JSON.stringify(op);

                    }
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'oi_id', title: __('Mdsi_id'), operate: false},
                        {field: 'oi_number', title: __('Mdsi_number'), operate: 'LIKE'},
                        {field: 'oi_year', title: __('Mdsi_year'), operate: 'LIKE'},
                        {field: 'oi_unit', title: __('Mdsi_unit'), operate: false},
                        {field: 'oi_cate_name', title: __('Mdsi_cate_name'), operate: 'LIKE'},
                        {field: 'oi_specs', title: __('Mdsi_specs'), operate: false},
                        {field: 'oi_is_fear_query', title: '是否怕查',searchList: {"0":'不怕查',"1":'怕查'}, formatter: Table.api.formatter.status },
                        {field: 'oi_purchase_price', title: __('Mdsi_purchase_price'),  operate: false},
                        {field: 'oi_price', title: __('Mdsi_price'), operate:'BETWEEN'},
                        {field: 'oi_num', title: __('Mdsi_num'), operate: false},
                        {field: 'oi_date', title: __('Mdsi_date'), operate: 'LIKE'},
                        {field: 'oi_batch', title: __('Mdsi_batch'), operate: 'LIKE'},
                        {field: 'oi_code', title: __('Mdsi_code'), operate: 'LIKE'},
                        {field: 'oi_bottle_code', title: __('Mdsi_bottle_code'), operate: 'LIKE'},
                        {field: 'oi_user_name', title: '客户', operate: false},
                        {field: 'supply_name', title: '供货商', operate: false},
                        {field: 'oi_create_time', title: '出库时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'oi_refund_time', title: '退库时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,

                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        out: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'select/storage/out' + location.search,
                    add_url: 'select/storage/add',
                    editall_url: 'select/storage/editall',
                    savestaff_url: 'select/storage/savestaff',
                    edit_url: 'select/storage/edit',
                    returnorderinfo_url: 'select/storage/refundOrderInfo',
                    //del_url: 'select/storage/del',
                    multi_url: 'select/storage/multi',
                    import_url: 'select/storage/import',
                    table: 'mt_dish_storage_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'oi_id',
                sortName: 'oi_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams:function(params){
                    var filter=JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var oi_order_id=Fast.api.query('oi_order_id');
                    if (oi_order_id){
                        filter.oi_order_id=oi_order_id;
                        op.oi_order_id='=';
                        params.filter=JSON.stringify(filter);
                        params.op = JSON.stringify(op);

                    }
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'oi_id', title: __('Mdsi_id'), operate: false},
                        {field: 'oi_number', title: __('Mdsi_number'), operate: 'LIKE'},
                        {field: 'oi_year', title: __('Mdsi_year'), operate: 'LIKE'},
                        {field: 'oi_unit', title: __('Mdsi_unit'), operate: false},
                        {field: 'oi_cate_name', title: __('Mdsi_cate_name'), operate: 'LIKE'},
                        {field: 'oi_specs', title: __('Mdsi_specs'), operate: false},
                        {field: 'oi_is_fear_query', title: '是否怕查',searchList: {"0":'不怕查',"1":'怕查'}, formatter: Table.api.formatter.status },
                        {field: 'oi_purchase_price', title: __('Mdsi_purchase_price'),  operate: false},
                        {field: 'oi_price', title: __('Mdsi_price'), operate:'BETWEEN'},
                        {field: 'oi_num', title: __('Mdsi_num'), operate: false},
                        {field: 'oi_date', title: __('Mdsi_date'), operate: 'LIKE'},
                        {field: 'oi_batch', title: __('Mdsi_batch'), operate: 'LIKE'},
                        {field: 'oi_code', title: __('Mdsi_code'), operate: 'LIKE'},
                        {field: 'oi_bottle_code', title: __('Mdsi_bottle_code'), operate: 'LIKE'},
                        {field: 'oi_user_name', title: '客户', operate: false},
                        {field: 'supply_name', title: '供货商', operate: false},
                        {field: 'oi_create_time', title: '出库时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            // buttons : [
                            //     {
                            //         name: 'cancel', text:'退库',title:'是否确认退库' ,  icon: 'fa',extend:'data-toggle="tooltip"', classname: 'btn btn-xs btn-ajax btn-primary btn-dialog', url: 'select/order/cancelOrder',
                            //         confirm: '是否确认退库?',
                            //         success: function (data, ret) {
                            //             $("#table").bootstrapTable('refresh', {});
                            //         },
                            //         error: function (data, ret) {
                            //             console.log(data, ret);
                            //             Layer.alert(ret.msg);
                            //         },
                            //         visible: function (row) {
                            //             return true;
                            //         },
                            //     },
                            // ]
                        }
                    ]
                ]
            });
            /**
             * 批量弹窗
             */
            $(document).on("click", ".btn-editall", function (e) {
                // 获取选中的列表ID
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.editall_url + "?ids=" + ids, '批量修改')
            });

            $(".btn-returnorderinfo").click(function() {
                var ids = Table.api.selectedids(table)
                //console.log(ids)
                if(ids.length>0)
                    layer.confirm("是否批量退库？", function() {
                        $.ajax({
                            type  : 'post',
                            url: $.fn.bootstrapTable.defaults.extend.returnorderinfo_url,
                            data: {
                                ids: ids,
                                type:1
                            },
                            success(res) {
                                if (res.code == 1) {
                                    layer.alert("取消成功！")
                                } else {
                                    layer.alert(res.msg)
                                }
                                table.bootstrapTable('refresh', {});
                            }
                        })
                    })
                else{
                    layer.alert("请选择退库产品")
                }
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        in: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'select/storage/in' + location.search,
                    add_url: 'select/storage/add',
                    editall_url: 'select/storage/editall',
                    savestaff_url: 'select/storage/savestaff',
                    edit_url: 'select/storage/edit',
                    del_url: 'select/storage/del',
                    multi_url: 'select/storage/multi',
                    import_url: 'select/storage/import',
                    table: 'mt_dish_storage_info',
                    outwarehouse_url: 'select/order/outwarehouse/type/storage',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'oi_id',
                sortName: 'oi_id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams:function(params){
                    var filter=JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var oi_order_id=Fast.api.query('oi_order_id');
                    if (oi_order_id){
                        filter.oi_order_id=oi_order_id;
                        op.oi_order_id='=';
                        params.filter=JSON.stringify(filter);
                        params.op = JSON.stringify(op);

                    }
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'oi_id', title: __('Mdsi_id'), operate: false},
                        {field: 'oi_number', title: __('Mdsi_number'), operate: 'LIKE'},
                        {field: 'oi_year', title: __('Mdsi_year'), operate: 'LIKE'},
                        {field: 'oi_unit', title: __('Mdsi_unit'),  operate: false},
                        {field: 'oi_cate_name', title: __('Mdsi_cate_name'), operate: 'LIKE'},
                        {field: 'oi_specs', title: __('Mdsi_specs'), operate: false},
                        {field: 'oi_is_fear_query', title: '是否怕查',searchList: {"0":'不怕查',"1":'怕查'}, formatter: Table.api.formatter.status },
                        {field: 'oi_price', title: __('Mdsi_price'), operate:'BETWEEN'},
                        {field: 'oi_num', title: __('Mdsi_num'), operate: false},
                        {field: 'oi_surplus_num', title: __('Mdsi_surplus_num'),operate: 'BETWEEN'},
                        {field: 'oi_date', title: __('Mdsi_date'), operate: 'LIKE'},
                        {field: 'oi_batch', title: __('Mdsi_batch'), operate: 'LIKE'},
                        {field: 'oi_code', title: __('Mdsi_code'), operate: 'LIKE'},
                        {field: 'oi_bottle_code', title: __('Mdsi_bottle_code'), operate: 'LIKE'},
                        {field: 'oi_user_name', title: '供货商', operate: false},
                        {field: 'oi_create_time', title: '入库时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(document).on("click", ".btn-savestaff", function (e) {
                // 获取选中的列表ID
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.savestaff_url + "?type=2&ids=" + ids, '批量设置员工')
            });
            $(document).on("click", ".btn-editall", function (e) {
                // 获取选中的列表ID
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.editall_url + "?ids=" + ids, '批量修改')
            });
            $(document).on("click", ".btn-outwarehouse", function (e) {
                // 获取选中的列表ID
                var ids = Table.api.selectedids(table);
                Fast.api.open($.fn.bootstrapTable.defaults.extend.outwarehouse_url + "?ids=" + ids, '后台出库')
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        editall: function () {
            Controller.api.bindevent();
        },
        outwarehouse: function () {
            Controller.api.bindevent();
        },
        savestaff: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
